package chapter09

import java.util.jar.Attributes.Name

/**
 * 隐式转换
 * 隐式函数
 */
object Demo02_Implicit {
  def main(args: Array[String]): Unit = {

//    val new12 = new MyRiceInt(12)
//    println(new12.myMax(15))

//    //隐式函数
//    implicit def convert(num:Int) : MyRiceInt = new MyRiceInt(num)
//    println(12.myMax(15))

    //隐式类
    implicit class MyRiceInt( val self: Int) {
      //自定义比较大小的方法
      def myMax(n : Int) : Int = if ( n<self ) self else n
      def myMin(n : Int) : Int = if ( n<self ) n else self
    }

    println(12.myMax(222))

    println("===============")

    //隐式参数
//    def sayHello(name: String = "alice") : Unit ={
//      println("hello , "  + name)
//    }
//    def sayHi(name: String = "alice") : Unit ={
//      println("hi , "  + name)
//    }
//    sayHello()
//    sayHi()

    implicit val str : String = " BOB"
    implicit val int : Int = 18
    def sayGood(implicit name  : String) : Unit  ={
      println("Good " + name )
    }
    def sayYe(implicit name : String ) : Unit = {
      println("Ye " + name)
    }
    sayGood
    sayYe

    //简便写法
//    def hiAge(implicit age: Int) : Unit = {
//      println("hi " + age)
//    }
//    hiAge
    def hiAge(): Unit = {
   println("hi " + implicitly[Int])
}
    hiAge()
  }
}
//class MyRiceInt( val self: Int) {
//  //自定义比较大小的方法
//  def myMax(n : Int) : Int = if ( n<self ) self else n
//  def myMin(n : Int) : Int = if ( n<self ) n else self
//}